using System.Data;
using MySqlConnector;

namespace Front._1118;

public partial class Bookform : Form
{
    public Bookform()
    {
        InitializeComponent();
    }

    private void BookForm_Load(object sender, EventArgs e)
    {
        //查询所有图书
        GetAllBooks();

        //查询所有分类
        GetAllCategories();
    }

    private void GetAllCategories()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=Zxcvbnm0000;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        String sql = "select distinct book_category from book_info;";
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        DataSet dataSet = new DataSet();
        mySqlDataAdapter.Fill(dataSet, "book_category");
        if (dataSet.Tables["book_category"].Rows.Count > 0)
        {
            DataTable categoryTable = dataSet.Tables["book_category"];
            
            // 插入一行全部
            DataRow row = categoryTable.NewRow();
            row[0] = "全部";
            categoryTable.Rows.InsertAt(row,0);
            comboBox1.DataSource = categoryTable;
            comboBox1.DisplayMember = categoryTable.Columns[0].ColumnName;
        }
        
    }

    private void GetAllBooks()
    {
        using var connection =
            new MySqlConnection(
                "server=localhost;port=3306;database=book;user=root;password=Zxcvbnm0000;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        String sql = "select * from book_info;";
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        DataSet dataSet = new DataSet();
        mySqlDataAdapter.Fill(dataSet, "book_info");
        this.dataGridView1.DataSource = dataSet.Tables["book_info"];
    }
    private void button2_Click(object sender, EventArgs e)
    {
        AddBookForm addForm = new AddBookForm();
        if (addForm.ShowDialog() == DialogResult.OK)
        {
            GetAllBooks(); // 新增成功后刷新列表
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        // 检查是否选中行
        if (dataGridView1.SelectedRows.Count == 0)
        {
            MessageBox.Show("请选择要删除的图书");
            return;
        }
        string isbn = dataGridView1.SelectedRows[0].Cells["isbn"].Value.ToString();
        DialogResult result = MessageBox.Show($"确定要删除ISBN为{isbn}的图书吗？", "确认删除", MessageBoxButtons.YesNo);
    
        if (result == DialogResult.Yes)
        {
            DeleteBook(isbn);
        }
    }
    private void DeleteBook(string isbn)
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=Zxcvbnm0000;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        try
        {
            connection.Open();
            string sql = "delete from book_info where isbn = @isbn";
            using var command = new MySqlCommand(sql, connection);
            command.Parameters.AddWithValue("@isbn", isbn);
        
            int count = command.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("删除成功");
                GetAllBooks(); // 重新加载图书列表
            }
            else
            {
                MessageBox.Show("删除失败，未找到该图书");
            }
        }
        catch (MySqlException ex)
        {
            MessageBox.Show($"删除失败：{ex.Message}");
        }
    }
}